import store from '../store'

//封装请求对象
const BASE_URL ='https://api.imooc-blog.lgdsunday.club/api'
// const BASE_URL ='http://localhost:3002/api'
function request({ url,data,method }){
	return new Promise((resolve,reject)=>{
		uni.request({
			url:BASE_URL + url,
			data,
			method,
			header:{ Authorization:store.state.user.token },
			success: ({ data }) => {
				if(data.success){
					//请求成功
					resolve(data)
				}else{
					//请求失败
					//给用户提示
					uni.showToast({
						title:data.messsage,
						icon:'none',
						mask:true,
						duration:3000					
					})
					reject(data.messsage)
				}
			},
			fail:error=>{
				reject(error)
			},
			complete: () => {
				uni.hideLoading()
			}
			
		})
	})
}
export default request